import {onBeforeMount, reactive, ref} from "vue";
import {gameModeMap, gameResultWinMap, queueMap} from "@/renderer/share/utils/constants.js";
import moment from "moment";
import {champion_store, map_store, staticAssertsStore} from "@/renderer/main-window/stores/storeAsserts.js";



export async function getSingleGameRecordInfo(param) {
   const  {summonerInfo,gameId}=param;

    //游戏对局详情
    let gameRecordDetail = {}

    //参与者ID和召唤师信息的分组
    const participantsummonerInfoMap = new Map();
    // 参与者id 和游戏数据分组
    const participantGameInfoMap = new Map();
    // puuid 对应的参与者id的分组
    const puuIdParticipantMap = new Map();
    //团队id和团队信息的分组
    const teamMap = new Map();
    //团队排序索引
    const teamMapSortMap = new Map();

    //要返回的数据
    let currentParticipantInfo = {}
    let participantInfos=[];
    //团队信息
    let totalTeamInfos = [];

//查询单个对局的战绩
    if (gameId === undefined) {
        return
    }

    function buildCurrentParticipantInfoFunc  () {
        const participantId = puuIdParticipantMap.get(summonerInfo.puuid);
        const player = participantsummonerInfoMap.get(participantId);
        return {
            "summonerInfo": player,
            "participantId": participantId,
            "info": buildSingleParticipantInfoFunc(summonerInfo.puuid)
        }
    }
    //查询数据

   await window.api.gamereocrd.searchGameDetailByGameId({
        "gameId": gameId,
    }).then(res => {
        gameRecordDetail = res.data;

        //计算map
        gameRecordDetail?.participantIdentities.forEach(item => {
            const {participantId, player} = item;
            const {puuid} = player;
            participantsummonerInfoMap.set(participantId, player);
            puuIdParticipantMap.set(puuid, participantId);
        })

        //游戏信息map
        gameRecordDetail?.participants.forEach(item => {
            participantGameInfoMap.set(item.participantId, item);
        })

        gameRecordDetail?.teams?.forEach((team, idx) => {
            teamMap.set(team.teamId, team);
            teamMapSortMap.set(team.teamId, idx);
        })

        currentParticipantInfo = {...buildCurrentParticipantInfoFunc()}
        participantInfos = buildTotalParticipantInfoFunc();
        totalTeamInfos=gameRecordDetail?.teams;
    })

//各种透给子模块的数据
//当前用户的数据




//用户的全部信息
    function buildTotalParticipantInfoFunc (){
        let arr = (gameRecordDetail?.participantIdentities);
        if (arr === undefined) {
            return [];
        }

        const result = [];

        arr.map(item => {
            const puuid = participantsummonerInfoMap.get(item.participantId)?.puuid
            return buildSingleParticipantInfoFunc(puuid);
        }).forEach(item => {
            if (gameRecordDetail?.gameMode === 'CHERRY') {
                const idx = item?.subteamPlacement - 1;
                if (!result[idx]) {
                    result[idx] = [];
                }
                result[idx].push(item);
            } else {
                const idx = teamMapSortMap.get(item?.teamInfo?.teamId);
                if (!result[idx]) {
                    result[idx] = [];
                }
                result[idx].push(item);
            }


        })
        return result;
    }





//能力
    function buildSingleParticipantInfoFunc (puuid) {
        const participantId = puuIdParticipantMap.get(puuid);
        const player = participantsummonerInfoMap.get(participantId);
        const {
            championId,
            spell1Id,
            spell2Id,
            teamId,
            stats
        } = (participantGameInfoMap.get(participantId)) ?? {};

        const {
            champLevel, //英雄等级
            goldEarned, //获取金币
            wardsKilled,//排眼
            visionScore,//视野得分
            visionWardsBoughtInGame,//真眼数量
            perkPrimaryStyle,//主天赋id
            perkSubStyle,//副天赋ID
            perk0,//主天赋 第一行
            perk0Var1,//主天赋 第1行
            perk0Var2,//主天赋 第2行
            perk0Var3,//主天赋 第3行,
            perk1,
            perk1Var1,
            perk1Var2,
            perk1Var3,
            perk2,
            perk2Var1,
            perk2Var2,
            perk2Var3,
            perk3,
            perk3Var1,
            perk3Var2,
            perk3Var3,
            perk4,
            perk4Var1,
            perk4Var2,
            perk4Var3,
            perk5,
            perk5Var1,
            perk5Var2,
            perk5Var3,
            subteamPlacement,//斗魂竞技场 队伍排名


        } = stats;
        const minute = (gameRecordDetail?.gameDuration / 60).toFixed(0)
        const second = gameRecordDetail?.gameDuration % 60;
        const teamInfo = teamMap.get(teamId);
        const championName = staticAssertsStore().getAssertSync({key: champion_store, id: championId})?.name
        const mapName = staticAssertsStore().getAssertSync({key: map_store, id: gameRecordDetail?.mapId})?.name


        return {
            "player": player,
            "champion": {
                "championId": championId,
                "championName": championName,
                "spell": {
                    d: spell1Id,
                    f: spell2Id
                },
                //天赋
                "perkStyle": {
                    perkPrimaryStyle: perkPrimaryStyle,
                    perkSubStyle: perkSubStyle,
                    perk0: perk0,
                    perk0Var1: perk0Var1,
                    perk0Var2: perk0Var2,
                    perk0Var3: perk0Var3,
                    perk1: perk1,
                    perk1Var1: perk1Var1,
                    perk1Var2: perk1Var2,
                    perk1Var3: perk1Var3,
                    perk2: perk2,
                    perk2Var1: perk2Var1,
                    perk2Var2: perk2Var2,
                    perk2Var3: perk2Var3,

                    perk3: perk3,
                    perk3Var1: perk3Var1,
                    perk3Var2: perk3Var2,
                    perk3Var3: perk3Var3,

                    perk4: perk4,
                    perk4Var1: perk4Var1,
                    perk4Var2: perk4Var2,
                    perk4Var3: perk4Var3,

                    perk5: perk5,
                    perk5Var1: perk5Var1,
                    perk5Var2: perk5Var2,
                    perk5Var3: perk5Var3
                },
                "items": {
                    item0: stats?.item0,
                    item1: stats?.item1,
                    item2: stats?.item2,
                    item3: stats?.item3,
                    item4: stats?.item4,
                    item5: stats?.item5,
                    item6: stats?.item6

                }
            },
            "data": {
                "kda": {
                    k: stats?.kills,
                    d: stats?.deaths,
                    a: stats?.assists
                },
                //总输出伤害
                "totalDamageDealtToChampions": stats?.totalDamageDealtToChampions,
                //总承受伤害
                "totalDamageTaken": stats?.totalDamageTaken,
                "champLevel": champLevel,
                "goldEarned": goldEarned,
                "wardsKilled": wardsKilled,//摧毁守卫
                "visionScore": visionScore,//视野得分
                "visionWardsBoughtInGame": visionWardsBoughtInGame,//购买真眼
            },
            "tags": [],
            "baseInfo": {
                "queue": {
                    "queueId": gameRecordDetail?.queueId,
                    "queueName": queueMap[gameRecordDetail?.queueId],
                    "gameMode": gameRecordDetail.gameMode,
                },
                mapInfo: {
                    mapId: gameRecordDetail?.mapId,
                    mapName: mapName,
                },
                "useTimeText": `${minute}分${second}秒`,
                "isWin": gameResultWinMap[teamInfo?.win],
                "win": teamInfo?.win,
                "gameStartTime": moment.unix(gameRecordDetail.gameCreation / 1000).format("MM-DD HH:mm"),
            },
            "teamInfo": teamInfo,
            subteamPlacement: subteamPlacement,
        }
    }

    return {
        currentParticipantInfo,
        participantInfos,
        totalTeamInfos
    }

}
